/*
 * Copyright 2013 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.controller.manage;

import java.util.ArrayList;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fujitsu.nanjing.tieba.base.BaseController;
import com.fujitsu.nanjing.tieba.exception.Resource404Exception;
import com.fujitsu.nanjing.tieba.ibator.CoreBoard;
import com.fujitsu.nanjing.tieba.ibator.CoreReply;
import com.fujitsu.nanjing.tieba.ibator.CoreTopic;
import com.fujitsu.nanjing.tieba.service.BoardService;
import com.fujitsu.nanjing.tieba.service.ReplyService;
import com.fujitsu.nanjing.tieba.service.SectionInfoService;
import com.fujitsu.nanjing.tieba.service.TopicService;

/**
 * ManageController.java
 * 
 * @author Administrator
 */
@Controller
@Scope
@RequestMapping("/forum/manage")
public class ManageController extends BaseController {
	@Resource
	private BoardService boardService;
	@Resource
	private TopicService topicService;
	@Resource
	private SectionInfoService sectionInfoService;
	@Resource
	private ReplyService replyService;

	private static final ArrayList<String> kinds = new ArrayList<String>();
	static {
		kinds.add("delete");
		kinds.add("move");
		kinds.add("close");
		kinds.add("digest");
		kinds.add("highLight");
		kinds.add("top");
		kinds.add("report");
		kinds.add("best");
	}
	@RequestMapping(value = "/{boardid}/{kind}", method = {RequestMethod.POST,
			RequestMethod.GET})
	public String show(Model model, @PathVariable String boardid,
			@PathVariable String kind, String chkTopicID, String replyId)
			throws Resource404Exception {
		boardid = decrypt(boardid);
		checkBoard(boardid);
		kind = checkKind(kind);
		if (kind.equals("move")) {
			model.addAttribute("sectionInfos", sectionInfoService.findAll());
		} else if (kind.equals("report")) {
			if (replyId == null || replyId.equals("0")) {
				CoreTopic topic = topicService.findById(chkTopicID);
				if (topic == null) {
					throw new Resource404Exception("该主题不存在或已被删除，无法举报！");
				}
				replyId = "0";
				model.addAttribute("topicTitle", topic.getTitle());
			} else {
				CoreReply reply = replyService.findById(replyId);
				if (reply == null) {
					throw new Resource404Exception("该回复不存在或已被删除，无法举报！");
				}
				model.addAttribute("replyTitle", reply.getTitle());
			}
			model.addAttribute("replyId", replyId);

		} else if (kind.equals("best")) {
			model.addAttribute("replyId", replyId);
		}
		model.addAttribute("boardid", boardid);
		model.addAttribute("type", kind);
		model.addAttribute("topicsCount", getTopicsCount(chkTopicID));
		model.addAttribute("topicIds", chkTopicID);
		return "/manage/manage";
	}
	@RequestMapping(value = "/{boardid}/delete", method = RequestMethod.PUT)
	public String delete(Model model, @PathVariable String boardid,
			String chkTopicID, String sendsms, String reason)
			throws Resource404Exception {
		boardid = decrypt(boardid);
		checkBoard(boardid);
		String[] topicids = getTopicsId(chkTopicID);
		boolean sendFlag = false;
		if (sendsms != null && sendsms.equals("yes")) {
			sendFlag = true;
		}
		topicService.deleteTopics(topicids, sendFlag, reason);
		return redirect("/forum/boards/" + encrypt(boardid));
	}
	@RequestMapping(value = "/{boardid}/move", method = RequestMethod.PUT)
	public String move(Model model, @PathVariable String boardid,
			String chkTopicID, String sendsms, String reason, String moveto)
			throws Resource404Exception {
		boardid = decrypt(boardid);
		checkBoard(boardid);
		String[] topicids = getTopicsId(chkTopicID);
		boolean sendFlag = false;
		if (sendsms != null && sendsms.equals("yes")) {
			sendFlag = true;
		}
		topicService.moveTopics(topicids, moveto, sendFlag, reason);
		return redirect("/forum/boards/" + encrypt(boardid));
	}
	@RequestMapping(value = "/{boardid}/highLight", method = RequestMethod.PUT)
	public String highLight(Model model, @PathVariable String boardid,
			String chkTopicID, String sendsms, String reason,
			String lightcolor, String expiredate) throws Resource404Exception {
		boardid = decrypt(boardid);
		checkBoard(boardid);
		String[] topicids = getTopicsId(chkTopicID);
		boolean sendFlag = false;
		if (sendsms != null && sendsms.equals("yes")) {
			sendFlag = true;
		}
		topicService.highLight(topicids, lightcolor, sendFlag, reason,
				parseDate(expiredate));
		return redirect("/forum/boards/" + encrypt(boardid));
	}
	@RequestMapping(value = "/{boardid}/close", method = RequestMethod.PUT)
	public String close(Model model, @PathVariable String boardid,
			String chkTopicID, String sendsms, String reason, String state)
			throws Resource404Exception {
		boardid = decrypt(boardid);
		checkBoard(boardid);
		String[] topicids = getTopicsId(chkTopicID);
		boolean sendFlag = false;
		if (sendsms != null && sendsms.equals("yes")) {
			sendFlag = true;
		}
		topicService.close(topicids, state, sendFlag, reason);
		return redirect("/forum/boards/" + encrypt(boardid));
	}
	@RequestMapping(value = "/{boardid}/top", method = RequestMethod.PUT)
	public String topScope(Model model, @PathVariable String boardid,
			String chkTopicID, String sendsms, String reason, String topScope,
			String expiredate) throws Resource404Exception {
		boardid = decrypt(boardid);
		checkBoard(boardid);
		String[] topicids = getTopicsId(chkTopicID);
		boolean sendFlag = false;
		if (sendsms != null && sendsms.equals("yes")) {
			sendFlag = true;
		}
		topicService.topScope(topicids, topScope, sendFlag, reason,
				parseDate(expiredate));
		return redirect("/forum/boards/" + encrypt(boardid));
	}
	@RequestMapping(value = "/{boardid}/digest", method = RequestMethod.PUT)
	public String topScope(Model model, @PathVariable String boardid,
			String chkTopicID, String sendsms, String reason, String isDigest)
			throws Resource404Exception {
		boardid = decrypt(boardid);
		checkBoard(boardid);
		String[] topicids = getTopicsId(chkTopicID);
		boolean sendFlag = false;
		if (sendsms != null && sendsms.equals("yes")) {
			sendFlag = true;
		}
		topicService.digest(topicids, isDigest, sendFlag, reason);
		return redirect("/forum/boards/" + encrypt(boardid));
	}
	@RequestMapping(value = "/{boardid}/report", method = RequestMethod.PUT)
	public String report(Model model, @PathVariable String boardid,
			String chkTopicID, String sendsms, String reason, String replyId)
			throws Resource404Exception {
		boardid = decrypt(boardid);
		checkBoard(boardid);
		boolean sendFlag = false;
		if (sendsms != null && sendsms.equals("yes")) {
			sendFlag = true;
		}
		if (replyId == null || replyId.equals("0")) {
			topicService.report(chkTopicID, sendFlag, reason);
		} else {
			CoreReply coreReply = replyService.findById(replyId);
			if (coreReply != null) {
				replyService.reportReply(coreReply, Integer.parseInt(boardid),
						new Date(), reason, getUserDetails().getUsername());
			}
		}
		return redirect("/topics/" + encrypt(chkTopicID));
	}
	@RequestMapping(value = "/{boardid}/best", method = RequestMethod.PUT)
	public String best(Model model, @PathVariable String boardid,
			String chkTopicID, String sendsms, String reason, String replyId,
			String isBest) throws Resource404Exception {
		boardid = decrypt(boardid);
		checkBoard(boardid);
		boolean sendFlag = false;
		if (sendsms != null && sendsms.equals("yes")) {
			sendFlag = true;
		}
		topicService.best(chkTopicID, sendFlag, reason, replyId, isBest);
		return redirect("/topics/" + encrypt(chkTopicID));
	}
	public void checkBoard(String boardid) throws Resource404Exception {
		CoreBoard board = boardService.findById(boardid);
		if (board == null) {
			throw new Resource404Exception("404,你懂的！");
		}
	}

	private String checkKind(String kind) {
		for (String kindO : kinds) {
			if (kindO.equalsIgnoreCase(kind)) {
				return kindO;
			}
		}
		return kinds.get(0);
	}
	private String[] getTopicsId(String chkTopicID) {
		return chkTopicID.split(",");
	}
	private int getTopicsCount(String chkTopicID) {
		if (chkTopicID == null)
			return 0;
		return getTopicsId(chkTopicID).length;
	}
}
